package com.wunu.smartlock1.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;

import com.wunu.smartlock1.MainActivity;
import com.wunu.smartlock1.R;
import com.wunu.smartlock1.model.AuthorizedMemberVo;
import java.util.List;
import java.util.ArrayList;
import com.wunu.smartlock1.adapter.AuthorizedMemberAdapter;
import com.wunu.smartlock1.net.NetComponent;
import com.wunu.smartlock1.net.api.LockManageApi;
import com.wunu.smartlock1.net.response.ApiNormalResponse;
import com.wunu.smartlock1.net.response.BindLockResult;
import com.wunu.smartlock1.net.response.LockMemberList;
import com.wunu.smartlock1.util.CommonUtil;

import android.widget.ListView;
import android.widget.Toast;

import retrofit2.Call;

/**
 * Created by admin on 2018/4/21.
 */

public class AuthorizedLockActivity extends AppCompatActivity {
    private int lockId;
    ProgressDialog pd;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            ApiNormalResponse resp = (ApiNormalResponse) msg.obj;
            switch (msg.what) {
                case 1:
                    pd.dismiss();
                    if (resp == null || resp.getCode() != 0
                            || resp.getData() == null || resp.getData().getCode() != 200) {
                        Toast.makeText(AuthorizedLockActivity.this, "获取授权成员列表失败!", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    LockMemberList list = (LockMemberList) resp.getData().getData();
                    if (!list.getOwner()) {
                        Toast.makeText(AuthorizedLockActivity.this, "非管理员!", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    List<AuthorizedMemberVo> auList = transMemberList(list);
                    AuthorizedMemberAdapter adapter = new AuthorizedMemberAdapter(AuthorizedLockActivity.this, R.layout.smartlock_activity_authorized_item, auList);
                    ListView listView = (ListView) findViewById(R.id.authorizedListView);
                    listView.setAdapter(adapter);
                    break;
                default:
                        break;
            }
        }
    };

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.smartlock_activity_authorizedlock);
        //构建一个下载进度条
        pd = ProgressDialog.show(AuthorizedLockActivity.this, "请等待", "正在查询授权成员列表…");
        List<AuthorizedMemberVo> list = getMemberList();
        //给button按钮绑定单击事件，设置完参数后跳转到首页

        //取得从上一个Activity当中传递过来的Intent对象
        Intent intent = getIntent();
        lockId = Integer.parseInt(intent.getStringExtra("lockId"));
        System.out.println("从主界面传递过来的授权成员lockId: "+lockId);
        new Thread() {
            public void run() {
                try {
                    LockManageApi service = NetComponent.getRetrofit().create(LockManageApi.class);
                    Call<ApiNormalResponse<LockMemberList>> call = service.getLockMemberList(lockId);
                    ApiNormalResponse<LockMemberList> resp = call.execute().body();
                    // 发送消息，把服务器返回的本文弹出吐司显示
                    Message msg = handler.obtainMessage();
                    msg.what = 1;//授权成员
                    msg.obj = resp;
                    handler.sendMessage(msg);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    public List<AuthorizedMemberVo> getMemberList() {
        List<AuthorizedMemberVo> list = new ArrayList<AuthorizedMemberVo>();
        for (int i = 0; i < 20; i++) {
            AuthorizedMemberVo obj = new AuthorizedMemberVo();
            obj.setAuthorizedType("认证成员");
            obj.setUserContent("包括管理员、家庭成员、保姆、钟点工等");
            obj.setAuthorizedName("柳岸花明");
            //obj.setAuthorizedPicUrl(R.mipmap.pic_dark);
            obj.setAuthorizedDescript("家庭成员");
            obj.setMemberType(0);
            obj.setMenberPicUrl(R.mipmap.members);
            obj.setUsedTimes((int) Math.random() % 200);
            obj.setLeftTimes((int) Math.random() % 100);
            list.add(obj);
        }
        AuthorizedMemberVo obj = new AuthorizedMemberVo();
        obj.setAuthorizedType("临时秘钥");
        obj.setUserContent("仅限管理员、家庭成员可授权");
        obj.setAuthorizedName("爱恨交戈");
        //obj.setAuthorizedPicUrl(R.mipmap.pic_love);
        obj.setAuthorizedDescript("已过期");
        obj.setMemberType(1);
        obj.setMenberPicUrl(R.mipmap.member);
        obj.setUsedTimes(100);
        obj.setLeftTimes(23);
        list.add(obj);
        return list;
    }


    public List<AuthorizedMemberVo> transMemberList(LockMemberList lockMemberList) {
        List<AuthorizedMemberVo> list = new ArrayList<AuthorizedMemberVo>();
        if (lockMemberList == null ||
                (lockMemberList.getLockMembers().size() == 0
                        && lockMemberList.getTempMembers().size() == 0)) {
            return list;
        }
        List<LockMemberList.LockMember> lockMembers = lockMemberList.getLockMembers();
        for (LockMemberList.LockMember member : lockMembers) {
            AuthorizedMemberVo obj = new AuthorizedMemberVo();
            obj.setAuthorizedType("认证成员");
            obj.setUserContent("包括管理员、家庭成员、保姆、钟点工等");
            obj.setAuthorizedName(member.getName());
            obj.setAuthorizedPicUrl(member.getPhoto());
            obj.setAuthorizedDescript("家庭成员");
            obj.setMemberType(0);
            obj.setMenberPicUrl(R.mipmap.members);
            //obj.setUsedTimes((int)Math.random()%200);
            //obj.setLeftTimes((int)Math.random()%100);
            list.add(obj);
        }

        List<LockMemberList.TempMember> tempMembers = lockMemberList.getTempMembers();
        for (LockMemberList.TempMember member : tempMembers) {
            AuthorizedMemberVo obj = new AuthorizedMemberVo();
            obj.setAuthorizedType("临时秘钥");
            obj.setUserContent("仅限管理员、家庭成员可授权");
            obj.setAuthorizedName(member.getName());
            obj.setAuthorizedPicUrl(member.getPhoto());
            if (!CommonUtil.compareDate(member.getStartTime(), member.getValidTime()))
                obj.setAuthorizedDescript("已过期");
            obj.setMemberType(1);
            obj.setMenberPicUrl(R.mipmap.member);
            obj.setUsedTimes(member.getTempKey());
            obj.setLeftTimes(member.getValidCount());
            list.add(obj);
        }
        return list;
    }
}
